<template>
  <div class="login_wrap">
    <div class="login_box">
      <div class="login_welcome">
        <h4>欢迎登陆</h4>
      </div>
      <div class="login_line"></div>
      <div class="login_form">
        <el-form
          class="demo-ruleForm"
          ref="loginForm"
          :model="user"
          :rules="rules"
        >
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" v-model="user.username">
              <template #append>
                <el-btn class="el-icon-user-solid"></el-btn>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="user.password"
            >
              <template #append>
                <el-btn class="el-icon-user-solid"></el-btn>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loginFn">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import {
  defineComponent,
  ref,
  reactive,
  onMounted,
  getCurrentInstance,
} from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { login } from "../../http/api";
export default defineComponent({
  setup() {
    const store = useStore();
    const router = useRouter();
    const { ctx } = getCurrentInstance();
    let user = reactive({
      username: "",
      password: "",
    });
    let rules = reactive({
      username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
      password: [{ required: true, trigger: "blur", message: "请输入密码" }],
    });
    let loginFn = () => {
      ctx.$refs.loginForm.validate((vali) => {
        if (vali) {
          login(user).then((res) => {
            store.commit("set_token", res.token);
            store.commit("set_username", res.username);
            router.push("/index");
          });
        }
      });
    };
    return {
      user,
      rules,
      loginFn,
    };
  },
});
</script>

<style scoped lang='scss'>
.login_wrap {
  width: 100vw;
  height: 100vh;
  background-image: url(https://file.iviewui.com/admin-dist/img/login-bg.0899ffa6.jpg);
  background-size: cover;
  position: relative;
}
.login_box {
  position: absolute;
  width: 300px;
  height: 250px;
  background-color: #fff;
  right: 50px;
  top: 50%;
  margin-top: -125px;
  border-radius: 15px;
  .login_welcome {
    width: 100%;
    height: 53px;
    position: relative;
    overflow: hidden;
    h4 {
      width: 260px;
      height: 16px;
      margin: 18px auto;
      line-height: 16px;
      text-align: left;
    }
  }
  .demo-ruleForm {
    padding: 10px 20px;
  }
}
</style>